<!--
  修改密码页面
-->
<template>
  <div>
    <el-row>
      <el-col :span=4 :push=10>
        <h1>修改密码</h1>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span=8 :push=8>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="原密码" prop="pre_pass">
            <el-input type="password" v-model="ruleForm.pre_pass" placeholder="请输入原密码" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入新密码" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请再次输入新密码" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item label="验证码:" prop="code">
            <el-input v-model="code" placeholder="请输入验证码" prefix-icon="el-icon-s-comment"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getCode" type="primary" plain :disabled="btnDisabled">{{getCodeText}}</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import instance from "../../utils/request";

export default {
  name: "PasswordUpt",
  data() {
    /*
    验证原密码正确性
     */
    var checkPre_pass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('原密码不能为空'));
      } else if (value !== this.$store.state.pswd) {
        callback(new Error('原密码错误!'));
      } else {
        callback();
      }
    };
    /*
    密码输入验证规则
     */
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    }
    /*
      密码输入验证规则
    */
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        pre_pass: '',
      },
      code:'',
      btnBool:false,
      resStatus:'false',
      btnDisabled:false,
      getCodeText:'获取验证码',
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        pre_pass: [
          { validator: checkPre_pass, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {

    submitForm(formName) {
      if(!this.ruleForm.pre_pass || !this.ruleForm.pass || !this.ruleForm.checkPass){
        alert('请完善输入信息');
        return;
      }
      let _this = this;
      instance.post('/api/worker/modifyPswd',{
        phoneNumber:_this.$store.state.id,
        oldPswd:_this.ruleForm.pre_pass,
        newPswd:_this.ruleForm.pass,
        code:_this.code
      }).then(function (res) {
        if(res.data.code === 200){
          alert('修改成功！');
          _this.$store.commit('changeLogin_Status');
          _this.$router.push('/index');     //修改成功跳转主页
        }
        else {
          if(res.data.code === 204){
            alert('验证码输入错误');
            _this.code = '';
          }
        }
      }).catch(function (err) {
        //异常处理逻辑
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取验证码方法
    getCode(){
      if(!this.ruleForm.pre_pass || !this.ruleForm.pass || !this.ruleForm.checkPass){
        alert('请完善输入信息');
        return;
      }
      let _this = this;
      instance.get('/api/worker/sendMessage/' + this.$store.state.id).then(function (res) {
        if(res.data.code === 200){
          alert('验证码发送成功');
          _this.btnBool = true;
          _this.resStatus = true;
          _this.btnDisabled = true;
          let i = 30;     //倒计时时间
          let timer = setInterval(() => {
            _this.getCodeText = i + "s";
            i--;
            if (i < 0) {
              _this.btnDisabled = false;
              _this.btnBool = false;
              _this.getCodeText = "获取验证码";
              clearInterval(timer);
            }
          }, 1000);
        }
        else if(res.data.code === 203 || !_this.resStatus){
          alert('验证码发送失败！');
        }
        else {
          console.log(res.data);
        }
      }).catch(function (err) {
        console.log(err);
      });
    }
  }
}
</script>

<style scoped>

</style>
